package com.srpingde.hhh.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HHHDB extends SQLiteOpenHelper {
	private static final int DB_VRESION = 1;
	// TODO 数据库文件名
	private static final String DB_NAME = "student.db";
	// TODO 数据库表名
	private static final String TABLE_NAME = "mystudent";
	public final static String FIELD_id = "_id";
	public final static String SHOPID = "shopid";
	public final static String IMAGE_PATH = "image_path";
	public final static String JIAGE = "jiage";
	public final static String KUANSHI = "kuanshi";
	Context c;

	/**
	 * 创建数据库访问对象 它实际上没有创建数据库，马上返回。
	 */
	public HHHDB(Context context) {
		super(context, DB_NAME, null, DB_VRESION);
		// TODO Auto-generated constructor stub
		this.c = context;
	}

	/**
	 * 第一次调用 getWritableDatabase() 或 getReadableDatabase() 时调用
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		String sql = "CREATE TABLE " + TABLE_NAME + "(" + FIELD_id
				+ " INTEGER	PRIMARY KEY autoincrement," + SHOPID + " VARCHAR(10) NULL," + IMAGE_PATH
				+ " VARCHAR(10) NULL," + JIAGE + " varchar(200) null,"+ KUANSHI +" VARCHAR(10) NULL"
				+ ")";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	// TODO 增加一条数据 方法
	public long insert(String shopID,String image_path, String jiage,String kuanshi) {
		SQLiteDatabase db = this.getWritableDatabase();
		/* 将新增的值放入ContentValues */
		ContentValues cv = new ContentValues();
		cv.put(SHOPID, shopID);
		cv.put(IMAGE_PATH, image_path);
		cv.put(JIAGE, jiage);
		cv.put(KUANSHI, kuanshi);
		/*cv.put(JIAGE, jiage);
		cv.put(COLOCR, colocr);*/
		long row = db.insert(TABLE_NAME, null, cv);
		// db.close();
		return row;
	}

	// TODO 删除一条数据 方法
	public void delete(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = FIELD_id + " = ?";
		String[] whereValue = { Integer.toString(id) };
		db.delete(TABLE_NAME, where, whereValue);
		db.close();
	}

	// TODO 更新一条数据 方法
	public void update(int id, String text, String text2) {
		SQLiteDatabase db = this.getWritableDatabase();
		String where = FIELD_id + " = ?";
		String[] whereValue = { Integer.toString(id) };
		/* 将修改的值放入ContentValues */
		ContentValues cv = new ContentValues();
		// cv.put(FIELD_names, text);
		// cv.put(FIELD_introductions, text2);
		db.update(TABLE_NAME, cv, where, whereValue);
		db.close();
	}

	// TODO 查询一条数据 方法
	public Cursor selectone(long rowId) {
		SQLiteDatabase db = this.getReadableDatabase();
		// Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null,
		// null);
		Cursor cursor = db.query(true, TABLE_NAME, new String[] { FIELD_id,SHOPID,
				IMAGE_PATH, JIAGE ,KUANSHI}, FIELD_id + "=" + rowId, null, null,
				null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		// cursor.close();
		db.close();
		return cursor;
	}

	// TODO 查询所有数据 方法
	public Cursor select() {
		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db
				.query(TABLE_NAME, null, null, null, null, null, null);
		return cursor;
	}
}
